import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLineEdit, QPushButton, QGridLayout


class Calculator(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("iPhone Style Calculator")
        self.setFixedSize(300, 400)
        self.setStyleSheet("""
            background-color: #f7f7f7;
            border-radius: 20px;
            padding: 20px;
        """)

        self.initUI()

    def initUI(self):
        layout = QVBoxLayout()

        self.display = QLineEdit('0')
        self.display.setFont(QFont('Arial', 24))
        self.display.setAlignment(Qt.AlignRight)
        self.display.setReadOnly(True)
        self.display.setStyleSheet("""
            background-color: black;
            color: white;
            border-radius: 15px;
            padding: 10px;
            font-weight: bold;
        """)
        layout.addWidget(self.display)

        grid_layout = QGridLayout()
        buttons = [
            ('AC', '#d3d3d3', '#a9a9a9'), ('±', '#d3d3d3', '#000000'), ('%', '#d3d3d3', '#000000'),
            ('÷', '#ffcc80', '#ff6f00'), ('7', '#333333', '#ffffff'), ('8', '#333333', '#ffffff'),
            ('9', '#333333', '#ffffff'),
            ('×', '#ffcc80', '#ff6f00'), ('4', '#333333', '#ffffff'), ('5', '#333333', '#ffffff'),
            ('6', '#333333', '#ffffff'),
            ('−', '#ffcc80', '#ff6f00'), ('1', '#333333', '#ffffff'), ('2', '#333333', '#ffffff'),
            ('3', '#333333', '#ffffff'),
            ('+', '#ffcc80', '#ff6f00'), ('0', '#333333', '#ffffff'), ('.', '#333333', '#ffffff'),
            ('=', '#ffcc80', '#ff6f00')
        ]

        positions = [(i, j) for i in range(4, 8) for j in range(4)]
        for position, button_info in zip(positions, buttons):
            btn = QPushButton(button_info[0])
            btn.setFont(QFont('Arial', 18))
            btn.setStyleSheet(f"""
                background-color: {button_info[1]};
                color: {button_info[2]};
                border-radius: 15px;
                min-width: 60px;
                min-height: 60px;
            """)
            btn.clicked.connect(lambda checked, b=btn_info[0]: self.on_click(b))

            if btn.text() == '0':
                grid_layout.addWidget(btn, *position, 1, 2)
            else:
                grid_layout.addWidget(btn, *position)

        layout.addLayout(grid_layout)
        self.setLayout(layout)

    def on_click(self, text):
        if text == '=':
            try:
                result = str(eval(self.display.text()))
                self.display.setText(result)
            except Exception as e:
                self.display.setText('Error')
        elif text == 'AC':
            self.display.clear()
            self.display.setText('0')
        elif text == '±':
            current_text = self.display.text()
            if current_text.startswith('-'):
                self.display.setText(current_text[1:])
            else:
                self.display.setText('-' + current_text)
        elif text == '%':
            try:
                value = float(self.display.text())
                percentage_value = value / 100
                self.display.setText(str(percentage_value))
            except ValueError:
                self.display.setText('Error')
        else:
            if self.display.text() == '0':
                self.display.setText(text)
            else:
                self.display.setText(self.display.text() + text)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    calc = Calculator()
    calc.show()
    sys.exit(app.exec_())
